package com.ledi.guns.core.intercept;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.ledi.guns.common.exception.ChangePassException;
import com.ledi.guns.common.exception.ReplaceException;
import com.ledi.guns.common.exception.RestSessionException;
import com.ledi.guns.core.base.controller.BaseController;
import com.ledi.guns.core.redis.KeyValueDAO;
import com.ledi.guns.core.shiro.ShiroKit;
import com.ledi.guns.core.shiro.ShiroUser;
import com.ledi.guns.core.support.HttpKit;
import com.ledi.guns.core.util.SystemConstans;

/**
 * 验证RestSession超时的拦截器
 *
 * @author fengshuonan
 * @date 2017年6月7日21:08:48
 */
@Aspect
@Component
@ConditionalOnProperty(prefix = "guns", name = "session-open", havingValue = "true")
public class RestSessionTimeoutInterceptor extends BaseController {

	private final static Logger logger = LoggerFactory.getLogger(RestSessionTimeoutInterceptor.class);
	@Autowired
	private KeyValueDAO keyValueDAO;

	@Pointcut("execution(* com.ledi.guns.modular.ledipay.controller.*.*(..))")
	public void cutService() {

	}

	@Around("cutService()")
	public Object sessionTimeoutValidate(ProceedingJoinPoint point) throws Throwable {

		String servletPath = HttpKit.getRequest().getServletPath();
		if (servletPath.equals("/kaptcha")
				|| servletPath.equals("/restApi/login")
				|| servletPath.equals("/restApi/logout")
				|| servletPath.equals("/restApi/kaptcha")
				|| servletPath.equals("/global/sessionError")
				|| servletPath.startsWith("/restApi/notify/")
				|| servletPath.startsWith("/restApi/user/")
				|| servletPath.startsWith("/restApi/scan/")
				|| servletPath.startsWith("/restApi/h5Apply")
				|| servletPath.startsWith("/file")
				|| servletPath.startsWith("/trigger/")) {
			return point.proceed();
		} else {
			if (ShiroKit.getSession().getAttribute("sessionFlag") == null) {
				ShiroKit.getSubject().logout();
				throw new RestSessionException();
			} else {
				ShiroUser user = ShiroKit.getUser();
				String key = SystemConstans.USER_ACOUNT_SESSIONID.replaceAll("\\{0\\}", user.getAccount());
				String string = keyValueDAO.get(key);

				String sessionId = HttpKit.getRequest().getSession().getId();
				keyValueDAO.set("sessionId", "1", 0);
				logger.info("当前已登录当前账号的sessionId：{}", string);
				logger.info("当前登录账号的sessionId:{}", sessionId);
				if (StringUtils.equals(string, sessionId)) {
					throw new ReplaceException();
				}
				String changeKey = SystemConstans.USER_SESSION_STATUS.replaceAll("\\{0\\}", HttpKit.getRequest().getSession().getId());
				String changeString = keyValueDAO.get(changeKey);
				if (StringUtils.equals(changeString, SystemConstans.USER_ACOUNT_STATUS_1)) {
					throw new ChangePassException();
				}
				String changeUser = SystemConstans.USER_SESSION_CHANGE.replaceAll("\\{0\\}", user.getAccount());
				String changeUserString = keyValueDAO.get(changeUser);
				if (StringUtils.equals(changeUserString, SystemConstans.USER_ACOUNT_STATUS_1)) {
					throw new ChangePassException();
				}
				return point.proceed();
			}
		}
	}
}
